/*
 * @描述: 角色管理数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { querySysRole, deleteSysRole, addSysRole, updateSysRole, exportSysRole, authorizeMenuToRole, querySysMenuTree, queryMenuIdByRoleId, querySysMenuButtonTree, queryMenuButtonByRoleCode, authorizeMenuButtonToRole } from '@/services/api';

export default {
  namespace: 'sysRole',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    menuTreeData: [],
    menuButtonTreeData: [],
    checkedMenuData: [],
    checkedMenuButtonData: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchMenuTree({ payload }, { call, put }) {
      const response = yield call(querySysMenuTree, payload);
      yield put({
        type: 'saveMenuTree',
        payload: response
      });
    },
    *fetchMenuButtonTree({ payload }, { call, put }) {
      const response = yield call(querySysMenuButtonTree, payload);
      yield put({
        type: 'saveMenuButtonTree',
        payload: response
      });
    },
    *fetchCheckedMenu({ payload }, { call, put }) {
      const response = yield call(queryMenuIdByRoleId, payload);
      yield put({
        type: 'saveCheckedMenu',
        payload: response
      });
    },
    *fetchCheckedMenuButton({ payload }, { call, put }) {
      const response = yield call(queryMenuButtonByRoleCode, payload);
      yield put({
        type: 'saveCheckedMenuButton',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addSysRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *authorizeMenu({ payload }, { call, put }) {
      const response = yield call(authorizeMenuToRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *authorizeMenuButton({ payload }, { call, put }) {
      const response = yield call(authorizeMenuButtonToRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteSysRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateSysRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *export({ payload }, { call, put }) {
      const response = yield call(exportSysRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveMenuTree(state, action) {
      return {
        ...state,
        menuTreeData: action.payload.data
      };
    },
    saveMenuButtonTree(state, action) {
      return {
        ...state,
        menuButtonTreeData: action.payload.data
      };
    },
    saveCheckedMenu(state, action) {
      return {
        ...state,
        checkedMenuData: action.payload.data
      };
    },
    saveCheckedMenuButton(state, action) {
      return {
        ...state,
        checkedMenuButtonData: action.payload.data
      };
    }
  }
};
